﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.Metadata.Entity.ScheduleJob;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.ScheduleJob.Dao
{
    public class ScheduleJobDataELBDao : DaoService<Metadata.Entity.ScheduleJob.ScheduleJobData>, IScheduleJobDataDao
    {
        public const string MetaDataID = "ScheduleJobData";
        public const string MetaDataName = "ScheduleJobData元数据参数";
        public override string TableName => "FAS_ScheduleJobData";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName
            => DaoUtil.GetExceptionColumn<Metadata.Entity.ScheduleJob.ScheduleJobData>(
                new List<string>
                {
                }
            );
        public ScheduleJobDataELBDao() : base()
        {
            DBName = "TFAS";
        }
        public override ScheduleJobData ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("ParamType"))
                columns.Remove("ParamType");
            if (columns.ContainsKey("ParamSourceType"))
                columns.Remove("ParamSourceType");
            if (columns.ContainsKey("IsNullable"))
                columns.Remove("IsNullable");
            if (columns.ContainsKey("IsList"))
                columns.Remove("IsList");

            var data = base.ConvertToObject(columns, row);

            data.ParamType = (ParamType)Enum.Parse(typeof(ParamType), Convert.ToString(row["ParamType"]));
            data.ParamSourceType = (ParamSourceType)Enum.Parse(typeof(ParamSourceType), Convert.ToString(row["ParamSourceType"]));
            data.IsNullable = DaoUtil.IntConvertBool(row["IsNullable"]);
            data.IsList = DaoUtil.IntConvertBool(row["IsList"]);

            return data;
        }

        public override Dictionary<string, object> GetDataMapping(ScheduleJobData data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("ParamType");
            mapping.Add("ParamType", (int)data.ParamType);

            mapping.Remove("ParamSourceType");
            mapping.Add("ParamSourceType", (int)data.ParamSourceType);

            mapping.Remove("ParamSourceType");
            mapping.Add("ParamSourceType", (int)data.ParamSourceType);
            return mapping;
        }

        public void AddScheduleJobData(ScheduleJobData jobData)
        {
            throw new NotImplementedException();
        }

        public int CheckUnique(Dictionary<string, string> filter)
        {
            throw new NotImplementedException();
        }

        public void DeleteScheduleJobData(string dataID)
        {
            throw new NotImplementedException();
        }

        public void DeleteScheduleJobDataByJobID(string sJobID)
        {
            var parameter = DB.MakeInParam("@ScheduleJobID", TDbDataType.VarChar, sJobID);
            DB.ExecSqlStatement(string.Format("delete from {0} where ScheduleJobID = @ScheduleJobID", TableName),parameter);
        }

        public List<ScheduleJobData> GetScheduleJobDataByFilter(Dictionary<string, string> filter)
        {
            throw new NotImplementedException();
        }

        public ScheduleJobData GetScheduleJobDataByID(string dataID)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 根据任务ID获取任务参数信息
        /// </summary>
        /// <param name="sJobID"></param>
        /// <returns></returns>
        public List<ScheduleJobData> GetScheduleJobDataByJobID(string sJobID)
        {
            var result = base.GetObjectsByFilter($"ScheduleJobID='{sJobID}'");
            var list = new List<ScheduleJobData>();
            if (result != null && result.Count > 0)
            {
                result.ForEach(x => list.Add(x as ScheduleJobData));
            }
            return list.OrderBy(x => x.SortOrder).ToList();
        }

        public void UpdateScheduleJobData(ScheduleJobData jobData)
        {
            throw new NotImplementedException();
        }
    }
}
